package com.hmall.item.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.constatnts.MqConstants;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.ItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/item")
public class ItemController {
    @Autowired
    private ItemService itemService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("list")
    public PageDTO<Item> itemList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                  @RequestParam(value = "size", defaultValue = "1") Integer size
    ) {
        Page<Item> result = itemService.page(new Page<>(page, size));
        return new PageDTO<>(result.getTotal(), result.getRecords());
    }

    @GetMapping("/{id}")
    public Item queryById(@PathVariable("id") Long id) {
        return itemService.getById(id);
    }

    @PostMapping("")
    public void item(@RequestBody Item item) {
        itemService.save(item);
    }

    @PutMapping("/status/{id}/{status}")
    public void status(@PathVariable("id") Long id,
                       @PathVariable("status") Integer status){
        Item item = new Item();
        item.setId(id);
        item.setStatus(status);
        itemService.updateById(item);
        if(status==1){
            rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE, MqConstants.HOTEL_INSERT_KEY, id);
        }else if(status==2){
            rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE,MqConstants.HOTEL_DELETE_KEY,id);
        }

    }
    @PutMapping("")
    public void update(@RequestBody Item item){
        itemService.updateById(item);
    }
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") Long id){
        itemService.removeById(id);


    }



}
